CC := gcc
CFLAGS := -Iinc -Wall -Wextra -O2
SRC_DIRS := src
OBJ_DIR := obj
LDFLAGS =

# 自动发现源文件
SRC := $(shell find $(SRC_DIRS) -name '*.c')
OBJ = $(addprefix $(OBJ_DIR)/,$(notdir $(SRC:.c=.o)))
TARGET := $(OBJ_DIR)/threads
DEPS = $(OBJECTS:.o=.d)

# VPATH for source lookup
VPATH = $(SRC_DIRS)

# Default target
all: $(TARGET)

# 链接阶段
$(TARGET): $(OBJ) | obj
	$(CC) $(LDFLAGS) $^ -o $@

# 编译规则（保留目录结构）
$(OBJ_DIR)/%.o: %.c | obj
	$(CC) $(CFLAGS) -c $< -o $@

# Create build directory
obj:
	mkdir -p $@

# Include dependencies
-include $(DEPS)

# Clean
.PHONY: clean
clean:
	rm -rf $(TARGET) $(OBJ) $(DEPS)

# Help
.PHONY: help
help:
	@echo "Usage:"
	@echo "  make     - build project"
	@echo "  make clean - remove build artifacts"

